package com.fayuan.nocv.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fayuan.nocv.entity.HeSuan;
import com.fayuan.nocv.entity.User;
import com.fayuan.nocv.entity.Vaccine;
import com.fayuan.nocv.service.VaccineService;
import com.fayuan.nocv.vo.DataView;
import com.fayuan.nocv.vo.VaccineVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/vaccine")
public class VaccineController {

    @Autowired
    private VaccineService vaccineService;

    @GetMapping("/loadAllVaccine")
    @ResponseBody
    public DataView loadAllVaccine(VaccineVo vaccineVo){

        IPage<Vaccine>page = new Page<>(vaccineVo.getPage(),vaccineVo.getLimit());

        QueryWrapper<Vaccine>wrapper = new QueryWrapper<>();
        if (vaccineVo.getName()!=null){
            wrapper.like("name",vaccineVo.getName());
        }
        IPage<Vaccine> page1 = vaccineService.page(page, wrapper);
        return new DataView(page1.getTotal(),page1.getRecords());
    }

    @GetMapping("/loadMyVaccine")
    @ResponseBody
    public DataView loadMyVaccine(VaccineVo vaccineVo, HttpSession session){

        IPage<Vaccine>page = new Page<>(vaccineVo.getPage(),vaccineVo.getLimit());

        QueryWrapper<Vaccine>wrapper = new QueryWrapper<>();
        User user = (User) session.getAttribute("user");
        wrapper.eq("name",user.getUsername());

        IPage<Vaccine> page1 = vaccineService.page(page, wrapper);
        return new DataView(page1.getTotal(),page1.getRecords());
    }

    @PostMapping("/addVaccine")
    @ResponseBody
    public DataView addVaccine(Vaccine vaccine) throws ParseException {

        Date createTime = vaccine.getCreateTime();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(createTime);
        Date parse = dateFormat.parse(format);

        vaccine.setCreateTime(parse);
        vaccineService.save(vaccine);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg(vaccine.getName()+"添加成功");
        return dataView;
    }

    @PostMapping("/updateVaccine")
    @ResponseBody
    public DataView updateVaccine(Vaccine vaccine) throws ParseException {

        Date createTime = vaccine.getCreateTime();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(createTime);
        Date parse = dateFormat.parse(format);

        vaccine.setCreateTime(parse);

        vaccineService.updateById(vaccine);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg(vaccine.getName()+"修改成功");
        return dataView;
    }

    @PostMapping("/deleteVaccine")
    @ResponseBody
    public DataView deleteVaccine(Vaccine vaccine){

        vaccineService.removeById(vaccine.getId());
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg(vaccine.getName()+"删除成功");
        return dataView;
    }
}
